package com.gxgeek.springaviator;

import com.googlecode.aviator.AviatorEvaluator;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.AbstractVariadicFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorDouble;
import com.googlecode.aviator.runtime.type.AviatorFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;

import java.util.Map;

/**
 * @author gaoxing
 * @create 2018-08-23 下午5:22
 */
public class AddFunction extends AbstractVariadicFunction {

    @Override
    public AviatorObject variadicCall(Map<String, Object> env, AviatorObject... args) {
//    public AviatorObject call(Map<String, Object> env, AviatorObject args1,AviatorObject args2) {
//        if (args.length != 2) {
//            throw new IllegalArgumentException("Add only supports two arguments");
//        }
        Number left = FunctionUtils.getNumberValue(args[0], env);
        Number right = FunctionUtils.getNumberValue( args[1], env);
        return new AviatorDouble(left.doubleValue() + right.doubleValue());
    }


    public String getName() {
        return "add";
    }

    public static void main(String[] args) {
        AviatorEvaluator.addFunction(new AddFunction());
        System.out.println(AviatorEvaluator.execute("add(1,2)"));
        System.out.println(AviatorEvaluator.execute("add(add(1,2),100)"));
    }

}
